package com.qf.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.test.pojo.DtsOrder;
import com.qf.test.pojo.DtsOrderGoods;
import com.qf.test.pojo.DtsUser;
import com.qf.test.service.OrderGoodsService;
import com.qf.test.service.OrderService;
import com.qf.test.util.ResponseUtil;
import com.qf.test.vo.OrederVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//page=1&limit=20&orderStatusArray=101&sort=add_time&order=desc&userId=1&orderSn=1
//page=1&limit=20&orderStatusArray=101&sort=add_time&order=desc&userId=1&orderSn=1
@RestController
@CrossOrigin("*")
@RequestMapping("/order")
public class OrdreController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderGoodsService orderGoodsService;


    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,

                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order,
                       String userId, String orderSn) {
        IPage<DtsOrder> list = orderService.findfall(page, limit, sort, order, userId, orderSn);
        Map<String, Object> data = new HashMap<>();
        //1.获取结果集
        List<DtsOrder> records = list.getRecords();
        //2.获取总条数
        long total = list.getTotal();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/listShipChannel")
    public Object listShipChannel() {
        List<OrederVo> list = orderGoodsService.findall();
        System.out.println(list.size());
        Map<String, Object> data = new HashMap<>();
        data.put("shipChannelList",list);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        List<DtsOrderGoods> all = orderGoodsService.findByOrderId(id);

        //1.通过用户id找到所有用户的数据，

        List<DtsUser> findname = orderGoodsService.findnameById(id);
        List<DtsOrder> findall = orderGoodsService.findByUserId();
        List<Object> objects = new ArrayList<>();
        objects.add(0, all);
        objects.add(1, findname);


        //响应数据
        Map<String, Object> data = new HashMap<>();
        //用户数据
        data.put("user", findall);
        //订单数据
        data.put("order", findall);
        //商品数据
        data.put("orderGoods", objects);
        return ResponseUtil.ok(data);
    }

}
